﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using TpBaseObjects.Objects;
using TpContracts;
using TpData.Manager;

namespace TpTimer {

    [ClassInterface(ClassInterfaceType.None)]
    [Guid("F7DEEAAC-E405-457A-A0B8-089133BB0D02")]

    [ProgId("TPTimer.MyTimer")]

    public class MyTimer:ITimer {

        #region Members

        private const string errType = "Err";
        private const string logType = "Log";
        private const string logArea = "TpTimer.MyTimer";

        #endregion

        #region Constructor

        public MyTimer() {
            TecUserObj u = TecUserDataManager.LoadTecUser(Environment.UserName);
            Contracts.SetMyUser(u);
        }

        #endregion

        #region Properties
        #endregion

        #region Methods

        public void OnStart(string fileName, int timerCount) {
            try {
                TimerFileObj tf = new TimerFileObj();
                tf.FileName = fileName;
                tf.SetDate = DateTime.Now;
                tf.TimerCount = timerCount;
                tf.UserID = Contracts.MyUser.UserID;

                TimerFileDataManager.SaveTimerFileObj(tf);
                LogDataManager.SaveLog(Contracts.MyUser.UserID, logType, logArea, "File is addedd (" + fileName + ", " + timerCount.ToString() + ")" , false);
            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "OnStart: " + ex.Message, true);
            }
        }

        public void OnStop(string fileName) {
            try {
                TimerFileDataManager.RemoveTimerFileObj(fileName, Contracts.MyUser.UserID);
                LogDataManager.SaveLog(Contracts.MyUser.UserID, logType, logArea, "File is removed (" + fileName + ")", false);
            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "OnStop: " + ex.Message, true);
            }
        }

        #endregion

    }
}
